package com.sakila.action.util;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 
 * @author SuperUni
 * any object returned with @ResponseBody annotation will be
 * converted to jackson automatically by spring mvc.
 * Note: each jquery ajax-table plugin has its own json display format(signature).
 *  	 Ex: 
 *  		Jtable     {"Result":"OK/ERROR", "Record":{one object}}
 *  				   {"Result":"OK/ERROR", "Records":[{object}, {object}...]}
 * 			jqGrid	   {"rows":[{object}, {object}...]}
 */
public class JsonUtil {
	/**
	 * jquery plugin: Easy UI
	 * @param Collection<T> data what you want to serialize to json
	 * @return map
	 */
	public static Object toJson(Object data){
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		map.put("rows", data);
		return map;
	}
	/**
	 * jquery plugin: Easy UI
	 * @param  Collection<T> data what you want to serialize to json
	 * @return map
	 */
	public static Object toJson(Object data, long numOfAll){
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		map.put("total", numOfAll);
		map.put("rows", data);
		return map;
	}
	
	/**
	 * jquery plugin: Jtable
	 * @param data what you want to serialize to json
	 * @return map
	 */
	public static Object toJson2(Object data){
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		map.put("Result", "OK");
		map.put("Records", data);
		return map;
	}
	
	/**
	 * jquery plugin: jqGrid
	 * @param data  data what you want to serialize to json
	 * @param numOfAll total rows count
	 * @param pageId   
	 * @param pageSize row size
	 * @return
	 */
	public static Object toJson(Object data, long numOfAll, int pageId, int pageSize){
		int totalPage = (int) Math.ceil(numOfAll/pageSize);
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		map.put("records", numOfAll);
		map.put("page", pageId);
		map.put("total", totalPage);
		map.put("rows", data);
		return map;
	}
}
